package engine;

import interfaces.IMassDistribution;

import java.util.ArrayList;

import joint.JointManager;
import joint.JointOperator;
import massDistribution.JointMassDistribution;
import massDistribution.MassDistribution;
import core.FrameOfDiscernment;
import exception.DempsterTotalConflictException;
import exception.JointNotPossibleException;
import exception.MassDistributionNotValidException;

public class TrustMAnager {

	/**
	 * Returns the aggregation mass between the reputation and the relationship
	 * mass using the {@link JointOperator} <code>operator</code>.
	 * 
	 * If one of the two masses is null or has completely lack on knowledge then
	 * it is returned only the one that has some information.
	 * 
	 * If one of the masses is null,null is returned.
	 * 
	 * If both of masses has the total lack of knowledge then a mas with a total
	 * lack of knowledge is returned.
	 * 
	 * 
	 * @param reputation
	 * @param relationship
	 * @param operator
	 * @param frame
	 * @return
	 * @throws CloneNotSupportedException
	 * @throws DempsterTotalConflictException
	 */
	public static MassDistribution getAggregationTrust(
			MassDistribution reputation, MassDistribution relationship,
			FrameOfDiscernment frame) throws CloneNotSupportedException,
			DempsterTotalConflictException {
		if (relationship != null && reputation != null) {

			// if (reputation.hasTotalLackOfKnowledge()
			// && relationship.hasTotalLackOfKnowledge()) {
			// return ServiceStub.getLackOfKnowledge(frame);
			// } else if (reputation.hasTotalLackOfKnowledge())
			// return relationship;
			// else if (relationship.hasTotalLackOfKnowledge())
			// return reputation;
			// else {
			// aggregate the two masses with the joint operator
			ArrayList<MassDistribution> masses = new ArrayList<MassDistribution>();
			masses.add(reputation);
			masses.add(relationship);
			try {
				JointMassDistribution agrregationTrust = JointManager
						.applyOperator(masses,
								((JointMassDistribution) reputation)
										.getOperator(), frame);
				return agrregationTrust;
			} catch (MassDistributionNotValidException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JointNotPossibleException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// }

		}

		return null;

	}

	public IMassDistribution getTrust(Service caller, Service callee) {
		// TODO
		return null;
	}

}
